package control;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.ChoiceDialog;
import javafx.scene.control.Dialog;
import javafx.scene.control.TextInputDialog;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

// 简单的对话框以及它的子类
public class DialogDemo extends Application {
	private VBox root = new VBox(50);
	private Button button1;
	private Button button2;
	private Button button3;
	private Button button4;
	
	public static void main(String[] args) {
		launch(args);
	}
	
	@Override
	public void start(Stage stage) {
		
		root.setAlignment(Pos.CENTER);
		
		button1 = new Button("Dialog");
		button2 = new Button("Alert");
		button3 = new Button("ChoiceDialog");
		button4 = new Button("TextInputDialog");
		root.getChildren().addAll(button1, button2, button3, button4);
		
		Scene scene = new Scene(root, 400, 400);
		stage.setScene(scene);
		stage.show();
		
		// 事件
		button1.setOnMouseClicked(e -> showDialog() );
		button2.setOnMouseClicked(e -> showAlert() );
		button3.setOnMouseClicked(e -> showChoiceDialog() );
		button4.setOnMouseClicked(e -> showTextInputDialog() );
	}
	
	// 显示对话框
	public void showDialog() {
		
		Dialog<ButtonType> dialog = new Dialog<>();		// 已经内定义为模态化窗口(关闭本窗口前不能与其他窗口交互)
//		dialog.initModality(Modality.APPLICATION_MODAL);
//		dialog.setOnCloseRequest(e -> e.consume() );	// 不能直接关闭(Dialog中不起作用)
		
		dialog.setHeaderText("我是标题");
		dialog.setContentText("我是内容");
		
		ImageView iv = new ImageView(new Image("image/警告.png"));
		iv.setFitWidth(100);
		iv.setFitHeight(100);
		dialog.setGraphic(iv);
		
		dialog.getDialogPane().getButtonTypes().add(ButtonType.OK);		// 还有很多类型
//		Button sure = (Button)dialog.getDialogPane().lookupButton(ButtonType.OK);	// setOnAction才有用
		dialog.getDialogPane().setPrefSize(300, 300);
		dialog.setTitle("对话框");
		dialog.show();
	}
	
	// 显示警告对话框
	public void showAlert() {
			
		Alert alert = new Alert(AlertType.WARNING);	// 默认有窗口标题，图片和确定按钮，内定义为模态窗口
			
		alert.setHeaderText("我是标题");
		alert.setContentText("我是内容");
		alert.getDialogPane().setPrefSize(300, 300);
		alert.show();
			
	}
	
	// 显示选择对话框
	public void showChoiceDialog() {
		
		ObservableList<String> choices = FXCollections.observableArrayList();	// 默认有图片,选择框,确定取消按钮
		choices.add("看书");
		choices.add("跑步");
		choices.add("吃饭");
		ChoiceDialog<String> cd = new ChoiceDialog<String>("看书", choices);		// 默认选项和选择列表
		cd.selectedItemProperty().addListener(e -> System.out.println(cd.getSelectedItem()));	// 输出选择的选项
		cd.show();
	}
	
	// 显示文本输入对话框
	public void showTextInputDialog() {
		
		TextInputDialog tid = new TextInputDialog("请输入");		// 默认有图片,输入框,确定取消按钮
		Button ok = (Button)tid.getDialogPane().lookupButton(ButtonType.OK);
		tid.show();
		
		// 输出输入框的内容(setOnAction才有用)
		ok.setOnAction(e -> System.out.println(tid.getEditor().getText()));
	}
}
